﻿namespace Liger.Web.UI
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Liger.Web.Common;
    using Liger.Web.Common.Extensions;

    public class GridColumnBuilder : JObjectBuilderBase, IHideObjectMembers
    {
        protected GridColumn column
        {
            get
            {
                return base.Obj as GridColumn;
            }
        }

        public GridColumnBuilder(GridColumn column)
            : base(column)
        { 
        }


        /// <summary>
        /// 是否可视
        /// </summary>
        public GridColumnBuilder Visible(bool value)
        {
            this.column.Visible = value;
            return this;
        }

        /// <summary>
        /// 绑定列名
        /// </summary>
        public GridColumnBuilder Name(string value)
        {
            this.column.Name = value;
            return this;
        }
        /// <summary>
        /// 显示列名
        /// </summary>
        public GridColumnBuilder Display(string value)
        {
            this.column.Display = value;
            return this;
        }

        /// <summary>
        /// 子列配置
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public GridColumnBuilder Columns(Action<GridColumnsBuilder> action)
        {
            if (action != null)
            {
                if (this.column.Columns == null)
                    this.column.Columns = new List<GridColumn>();
                action(new GridColumnsBuilder(this.column.Columns));
            }
            return this;
        }


        /// <summary>
        /// 对齐
        /// </summary>
        public GridColumnBuilder Align(string value)
        {
            this.column.Align = value;
            return this;
        }
        /// <summary>
        /// 宽度
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public GridColumnBuilder Width(int? value)
        {
            if (value.HasValue)
                Width(value.Value);
            return this;
        }
        /// <summary>
        /// 宽度
        /// </summary>
        public GridColumnBuilder Width(int value)
        {
            this.column.Width = value.ToString();
            return this;
        }
        /// <summary>
        /// 宽度
        /// </summary>
        public GridColumnBuilder Width(string value)
        {
            this.column.Width = value;
            return this;
        }
        /// <summary>
        /// 最小宽度
        /// </summary>
        public GridColumnBuilder MinWidth(string value)
        {
            this.column.MinWidth = value;
            return this;
        }
        /// <summary>
        /// 最大宽度
        /// </summary>
        public GridColumnBuilder MaxWidth(string value)
        {
            this.column.MaxWidth = value;
            return this;
        }
        /// <summary>
        /// 渲染器
        /// </summary>
        public GridColumnBuilder Render(string functionName)
        {
            this.column.Render = () => functionName;
            return this;
        }
        /// <summary>
        /// 渲染器
        /// </summary>
        public GridColumnBuilder Render(Func<string> function)
        {
            this.column.Render = function;
            return this;
        }
         
        
        /// <summary>
        /// 列类型
        /// </summary>
        public GridColumnBuilder Type(string value)
        {
            this.column.Type = value;
            return this;
        }
        /// <summary>
        /// 格式化
        /// </summary>
        public GridColumnBuilder Format(string value)
        {
            this.column.Format = value;
            return this;
        }
        /// <summary>
        /// 是否可以排序
        /// </summary>
        public GridColumnBuilder IsSort(bool value)
        {
            this.column.IsSort = value;
            return this;
        }

         /// <summary>
        /// 编辑器
        /// </summary>
        public GridColumnBuilder Editor()
        {
            return Editor("text");
        }
        /// <summary>
        /// 编辑器
        /// </summary>
        /// <param name="type">编辑器类型</param>
        /// <returns></returns>
        public GridColumnBuilder Editor(string type)
        {
            if (this.column.Editor == null)
                this.column.Editor = new GridColumnEditor();
            this.column.Editor.Type = type;
            return this;
        }
        /// <summary>
        /// 编辑器
        /// </summary>
        /// <param name="action">编辑器构造</param>
        /// <returns></returns>
        public GridColumnBuilder Editor(Action<GridColumnEditorBuilder> action)
        {
            if (action != null)
            {
                if (this.column.Editor == null)
                    this.column.Editor = new GridColumnEditor();
                action(new GridColumnEditorBuilder(this.column.Editor));
            } 
            return this;
        }
    }
}
